package com.mangosteen.interviewji.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mangosteen.interviewji.common.BaseResponse;
import com.mangosteen.interviewji.common.ErrorCode;
import com.mangosteen.interviewji.common.ResultUtils;
import com.mangosteen.interviewji.exception.BusinessException;
import com.mangosteen.interviewji.exception.ThrowUtils;
import com.mangosteen.interviewji.model.entity.User;
import com.mangosteen.interviewji.model.vo.QuestionVO;
import com.mangosteen.interviewji.service.QuestionFavourService;
import com.mangosteen.interviewji.service.QuestionThumbService;
import com.mangosteen.interviewji.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@Slf4j
@RequestMapping("/questionThumb")
public class QuestionThumbController {
    @Resource
    private UserService userService;

    @Resource
    private QuestionThumbService questionThumbService;
    @PostMapping("/doQuestionThumb")
    public BaseResponse<Boolean> doQuestionThumb(@RequestParam Long questionId, HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        return ResultUtils.success(questionThumbService.submit(questionId,loginUser.getId()));
    }
    @GetMapping("/get/UserThumb")
    public BaseResponse<Page<QuestionVO>> getUserThumb(@RequestParam Long current, @RequestParam Long size, HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        if (loginUser == null){
            throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
        }
        // 限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);

        return ResultUtils.success(questionThumbService.getUserThumb(current,size,loginUser.getId(), request));
    }
}
